<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>字符串练习</title>
  </head>

  <body>
    <script>
      // 1. 封装一个函数实现字符串翻转。
      var str = "assafsf";

      function reverse(str) {
        return str.split("").reverse().join("");
      }

      function reversestring(str) {
        let reverseStr = "";
        for (let i = str.length - 1; i >= 0; i--) {
          reverseStr += str[i];
        }
        return reverseStr;
      }

      // 2. 字符串 digite = "0123456789", 把前 5 个字符分割出来， 返回个单字符的数组, 注意， 返回数组里面的数字是字符串类型, string.split()
      let digite = "0123456789";
      let t = digite.substring(0, 5);
      let string = t.split("");
      console.log(string);

      // 3.已知有字符串 foo=”get-element-by-id”,写一个 function 将其转化成驼峰表示法”getElementById”.
      let str3 = "get-element-by-id";
      function split(str3) {
        let str4 = str3.split("-");
        let cases = str4.map((str) => {
          return str.chartAt(0).toUpperCase + str.slice(1);
        });
        return cases.join("");
      }

      // 4.定义一个函数,将"今天是什么鬼天气，这么热，明天要去游泳！"里面的有"天"的下标输出(用 indexof())

      let str4 = "今天是什么鬼天气，这么热，明天要去游泳！";
      function indexOf(str4) {
        let includes = [];
        let word = "天";
        while ((index = str4.indexOf(word) != -1)) {
          includes.push(index);
          str4 = str4.slice(index + word.length);
        }
        return includes;
      }

      console.log(includes);

      // 5.定义一个函数,将"今天是什么鬼天气，这么热，明天要去游泳！"里面的有"天"替换成"日"(用 replace())

      let str5 = "今天是什么鬼天气，这么热，明天要去游泳！";
      function replace() {
        let news = str5.replace(/天/g, "日");
        return news;
      }

      // 6.解析 URL Params 为对象，例如(https://www.baidu.com/s?ie=UTF-8&wd=letcode)

      // 返回结构如下:
      // {
      //     ie:"UTF-8",
      //     wd:"letcode"
      // }

      let url = "https://www.baidu.com/s?ie=UTF-8&wd=letcode";

      function parse(url) {}
      let Params = new URLSearchParams(url);
      let obj = {};
      Params.forEach((value, key) => {
        obj[key] = value;
      });

      return obj;

      // 7.查找字符串中(abbcccddddd)出现最多的字符和个数

      let str7 = "abbcccddddd";
      function findmosy(str7) {
        let charC = {};
        let matchs = str7.match(/abbcccddddd|[a-z]/g);
        for (let match of matchs) {
          for (let char of match) {
            if (charC[char]) {
              charC[char]++;
            } else {
              charC[char] = 1;
            }
          }
        }

        let max = 0;
        let most = "";
        for (let char in charC) {
          if (charC[char] > max) {
            max = charC[char];
            most = char;
          }
        }
        return { char: most, count: max };
      }

      // 8. "hi javascript hi hello world!" 改成 "Hi Javascript! Hi Hello World!"

      var str = "hi javascript hi hello world!";
      var newStr = str.replace(/javascript/g, "Javascript");
      newStr = newStr.replace(/hello/g, "Hello");
      newStr = newStr.replace(/world/g, "World");
      console.log(newStr);

      let str8 = "hi javascript hi hello world!";

      // 9. 把 "hi javascript!" 改成 "Hi Javascript!"
      let str9 = "hi javascript";

      //10. 定义一个函数, 函数功能是检测字符串是否包含指定字符, (指定字符可以支持多个, 只要包含其中一个指定字符即返回结果)

      function conain(inputString, chars) {
        var charArray = chars.split("");
        for (var i = 0; i < charArray.length; i++) {
          if (inputString.includes(charArray[i])) {
            return true;
          }
        }
        return false;
      }
    </script>
  </body>
</html>
